package com.mystyle.le.weekproject.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.asynctaks.PictureAsyncTaks;
import com.mystyle.le.weekproject.beans.Boutiques;
import com.mystyle.le.weekproject.beans.Manchester;
import com.mystyle.le.weekproject.intentfactbackcall.MyCallBack;
import com.mystyle.le.weekproject.utils.FileUtils;

import java.util.List;

/**
 * 精品界面
 * Created by le on 2016/10/10.
 */
public class BoutiqueAdapter extends BaseAdapter {
    private List<Boutiques> list;
    private Context context;

    public BoutiqueAdapter(List<Boutiques> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.boutique_pic3,parent,false);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.boutique_iv3);
            holder.textView = (TextView) convertView.findViewById(R.id.boutique_tv3);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Boutiques manchester = list.get(position);
        holder.textView.setText(manchester.getName());
        String icon = manchester.getIcon();//图片地址
        holder.imageView.setImageResource(R.mipmap.bottombar_game_pressed);
        holder.imageView.setTag(icon);
        if (FileUtils.readImg(icon)!=null){
            holder.imageView.setImageBitmap(FileUtils.readImg(icon));
        }else {
            new PictureAsyncTaks(new MyCallBack.DownPicture() {
                @Override
                public void downPicture(byte[] buf, String imgPath) {
                    if (imgPath.equals(holder.imageView.getTag())){
                        FileUtils.saveImage(imgPath,buf);//保存图片到SD卡
                        Bitmap bitmap = BitmapFactory.decodeByteArray(buf,0,buf.length);
                        holder.imageView.setImageBitmap(bitmap);
                    }
                }
            },context).execute(icon);
        }
        return convertView;
    }
    class ViewHolder{
        ImageView imageView;
        TextView textView;
    }

}
